<template>
	<view>
		<searchBar @search="toSearch"></searchBar>

		<!-- 朋友列表 -->
	
		<friendsListBlock :list="filterFriendList" :isSearchResult="!!searchKey" @itemClk="toShare"></friendsListBlock>

		<!-- 右侧字母表 -->
		<letterList></letterList>

		<!-- 分享评论提醒弹窗 -->
		<confirmPop :title="'分享'"  :btns="confirmPopBtns" :content="confirmPopContent" @maskClk="closeConfrimPop" @btnClk="confrimPopBtnClk"
			v-show="confirmPopShow" >
		</confirmPop>
		
		<!-- 新用户提醒弹窗 -->
		<alertPop :title="'温馨提示'" :content="alertPopContent" :btnText="'我知道了'"
			@maskClk="closeAlertPop" @btnClk="closeAlertPop" v-show="alertPopShow">
		</alertPop>

	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data() {
			return {
				confirmPopShow: false,
				confirmPopContent: '',
				fromName:'',
				fromId:'',
				alertPopContent:'',
				alertPopShow: false,
				searchKey:'',
				confirmPopBtns:[{
					text: '知道了',
					type: 'confirm'
				}, {
					text: '取消',
					type: 'cancel',
					bgColor:'#E5E5E5'
				}]
			}
		},
		computed: {
			...mapState(['friendList','userInfo']),
			filterFriendList(){
				return this.friendList.filter((item)=>{
					return item.userId != this.fromId && item.userId != this.userInfo.id
				})
			}
		},
		onLoad(options){
			this.fromName = options.fromName
			this.fromId = options.fromId
			this.commentId =  options.commentId
		},
		mounted(){
			this.$store.dispatch('getFriendList')
		},
		methods: {
			toSearch(key){
               this.searchKey = key
			   this.$store.dispatch('getFriendList', key? { keywords: key} : {})
			},
			toAddFriendsByPhone() {
				uni.navigateTo({
					url: '/pages/myFriends/addFriendsByPhone'
				})
			},
			showConfrimPop() {
				this.confirmPopShow = true
			},
			confrimPopBtnClk(item = {}) {
				if (item.type === 'confirm') {
				    this.confirmToShare()
				}
				if (item.type === 'cancel') {
				
				}
				this.closeConfrimPop()
			},
			closeConfrimPop() {
				this.confirmPopShow = false
			},
			toShare(item){
				this.toName = item.userName
				this.toId = item.userId
				const currentUserId = this.userInfo.id
				if(currentUserId != this.fromId){
					this.confirmPopBtns[0].text = '知道了'
					this.confirmPopContent = `点评分享给【${this.toName}】前，须先得到点评人【${this.fromName}】的同意。`
				}else{
					// 把自己的点评分享给别人 就不用再经过自己同意了
					this.confirmPopBtns[0].text = '确定'
					this.confirmPopContent = `是否将该点评分享给【${this.toName}】？`
				}	
				this.showConfrimPop()
			},
			confirmToShare(){
				this.$api.shareComment({
					fromUserId: this.userInfo.id,
					toUserId: this.toId,
					evaluateId: this.commentId*1
				}).then(res=>{
					if(this.userInfo.id != this.fromId){
						this.alertPopContent = `已向【${this.fromName}】发送消息，待其同意后点评将会直接分享给【${this.toName}】`
						this.alertPopShow = true
					}else{
						// 把自己的点评分享给别人 就不用再经过自己同意了
						uni.showToast({
							title: '分享成功',
							icon: 'none'
						})
					}
					
				})
			},
			closeAlertPop(){
				this.alertPopShow = false
			}
		}
	}
</script>


<style lang="less">
	
</style>
